<template>
  <div>
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>培训名称</p>
          <el-input v-model="qo.Filter" placeholder="请输入培训名称" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="6">
          <p>培训时间</p>
          <el-date-picker
            v-model="chooseTime"
            size="mini"
            type="daterange"
            style="width:100%"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleTimeChange"
          />
        </el-col>
        <el-col :span="4" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="tableLoading" :data="tableData" class="gl_table_noBorder gl_mt_15" size="mini" style="width: 100%;">
      <el-table-column label="序号" type="index" width="70" />
      <el-table-column label="培训LOGO" align="center" :width="95">
        <template slot-scope="{ row }">
          <img class="game_coverimg" :src="getStaticPhotoUrl(row.logoUrl)" alt="" @error="errorPhoto">
        </template>
      </el-table-column>
      <el-table-column label="培训名称" prop="name" :min-width="220" />
      <el-table-column label="培训地址" prop="address" :width="180" />
      <el-table-column label="培训类型" prop="type.name" align="center" :min-width="100">
        <template slot-scope="{ row }">
          {{ row.trainningType && row.trainningType.name }}
        </template>
      </el-table-column>
      <el-table-column label="培训时间" align="center" :min-width="170">
        <template slot-scope="{ row }">
          {{ row.startTime && row.startTime.substring(0, 10) }} - {{ row.endTime && row.endTime.substring(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="培训状态" align="left" :min-width="110">
        <template slot-scope="{ row }">
          <span v-if="row.statusCode === 'Entry'" class="gl_color_success">
            <i class="el-icon-success" />
            <span class="gl_pl_5">{{ row.trainningStatus && row.trainningStatus.name }}</span>
          </span>
          <span v-else-if="row.statusCode === 'Running'" class="gl_color_info">
            <i class="el-icon-success" />
            <span class="gl_pl_5">{{ row.trainningStatus && row.trainningStatus.name }}</span>
          </span>
          <span v-else class="gl_color_grey">
            <i class="el-icon-success" />
            <span class="gl_pl_5">{{ row.trainningStatus && row.trainningStatus.name }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('table.actions')" align="left" width="100">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="JumpToWebsite(row)">培训官网</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="gl_pt_15" style="text-align: right;">
      <el-pagination
        :current-page="qo.Page"
        :page-size="qo.MaxResultCount"
        :total="total"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>
  </div>
</template>

<script>
import defaultGameLogo from '@/assets/images/game/game_logoDefaultPhoto.png';
import {
  Api_GET_ClubActivityTopTrainingsQO // 获取当前道馆的推荐培训列表
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubActivityTopTrainings // 获取当前道馆的推荐培训列表
} from '@/extend/api/hallswaggerApi/app';
export default {
  data() {
    return {
      defaultGameLogo: defaultGameLogo,
      chooseTime: [],
      qo: new Api_GET_ClubActivityTopTrainingsQO(),
      tableLoading: false,
      total: 0,
      tableData: [],
      gameType: ''
    };
  },
  created() {
    this.getPageList();
  },
  methods: {
    // 获取培训列表
    getPageList(page) {
      this.qo.Page = page || 1;
      this.qo.SkipCount = (this.qo.Page - 1) * this.qo.MaxResultCount;
      this.qo.IsPublished = true;
      this.qo.IncludeTrainning = true;
      this.qo.IncludeTrainningStatus = true;
      this.qo.IncludeTrainningType = true;
      this.tableLoading = true;
      api_GET_ClubActivityTopTrainings(this.qo).then(res => {
        this.tableLoading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 跳转到培训官网
    JumpToWebsite(gameInfo) {
      var a = document.createElement('a');
      var event = new MouseEvent('click');
      a.href = `${location.origin}/train.html#/home/${gameInfo.route + (this.tenantId ? '_' + this.tenantId : '')}/info`;
      a.target = '_blank';
      a.dispatchEvent(event);
    },
    handleTimeChange(time) {
      this.qo.StartTime = time ? time[0] + ' 00:00:00' : null;
      this.qo.EndTime = time ? time[1] + ' 23:59:59' : null;
      this.getPageList();
    },
    clickReset() {
      this.chooseTime = [];
      this.qo = new Api_GET_ClubActivityTopTrainingsQO();
      this.getPageList();
    },
    // 获取图片地址
    getStaticPhotoUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultGameLogo;
    },
    // 分页
    sizeChange(MaxResultCount) {
      this.qo.MaxResultCount = MaxResultCount;
      this.getPageList();
    },
    pageIndexChange(currentPage) {
      this.getPageList(currentPage);
    }
  }
};
</script>
<style lang="scss" scoped>
.current_mainclub {
  font-size: 16px;
  .mainclub_name {
    font-weight: bold;
    display: inline-block;
    min-width: 100px;
    margin-right: 15px;
  }
}
.game_coverimg {
  max-width: 74px;
  max-height: 74px;
  border-radius: 50%;
  vertical-align: middle;
}
</style>
